//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules.Definitions;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Security;
using DotNetNuke.Security.Permissions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Tokens;
using DotNetNuke.Entities.Content;
using DotNetNuke.Entities.Tabs;

namespace DotNetNuke.Entities.Modules
{
	[XmlRoot("module", IsNullable = false)]
	[Serializable()]
    public class ModuleInfo : ContentItem, IHydratable, IPropertyAccess
	{
		private int _PortalID;
		private string _ModuleTitle;
		private bool _AllTabs;
		private bool _IsDeleted;
		private bool _InheritViewPermissions;
		private string _Header;
		private string _Footer;
		private System.DateTime _StartDate;
		private System.DateTime _EndDate;
		private int _TabModuleID;
		private string _PaneName;
		private int _ModuleOrder;
		private int _CacheTime;
		private string _CacheMethod;
		private string _Alignment;
		private string _Color;
		private string _Border;
		private string _IconFile;
		private VisibilityState _Visibility;
		private string _ContainerSrc;
		private bool _DisplayTitle;
		private bool _DisplayPrint;
		private bool _DisplaySyndicate;
		private bool _IsWebSlice;
		private string _WebSliceTitle;
		private DateTime _WebSliceExpiryDate;
		private int _WebSliceTTL;
	    private TabInfo _ParentTab;
		private int _DesktopModuleID;
		private DesktopModuleInfo _DesktopModule;
		private int _ModuleDefID;
		private ModuleDefinitionInfo _ModuleDefinition;
		private int _ModuleControlId;
		private ModuleControlInfo _ModuleControl;
		private string _AuthorizedEditRoles;
		private string _AuthorizedViewRoles;
		private string _AuthorizedRoles;
		private ModulePermissionCollection _ModulePermissions;
		private Hashtable _ModuleSettings;
		private Hashtable _TabModuleSettings;
		private TabPermissionCollection _TabPermissions;
		private string _ContainerPath;
		private int _PaneModuleIndex;
		private int _PaneModuleCount;
		private bool _IsDefaultModule;
		private bool _AllModules;
	    private Guid _UniqueId;
	    private Guid _VersionGuid;

        //Localization Properties
	    private string _CultureCode;
	    private Guid _DefaultLanguageGuid;
	    private Guid _LocalizedVersionGuid;
	    private ModuleInfo _DefaultLanguageModule;
	    private Dictionary<string, ModuleInfo> _LocalizedModules; 

		public ModuleInfo()
		{
			_PortalID = Null.NullInteger;
			_TabModuleID = Null.NullInteger;
			_DesktopModuleID = Null.NullInteger;
			_ModuleDefID = Null.NullInteger;
			_ModuleTitle = Null.NullString;
			_AuthorizedEditRoles = Null.NullString;
			_AuthorizedViewRoles = Null.NullString;
			_Alignment = Null.NullString;
			_Color = Null.NullString;
			_Border = Null.NullString;
			_IconFile = Null.NullString;
			_Header = Null.NullString;
			_Footer = Null.NullString;
			_StartDate = Null.NullDate;
			_EndDate = Null.NullDate;
			_ContainerSrc = Null.NullString;
			_DisplayTitle = true;
			_DisplayPrint = true;
			_DisplaySyndicate = false;

            //Guid, Version Guid, and Localized Version Guid should be initialised to a new value
		    _UniqueId = Guid.NewGuid();
		    _VersionGuid = Guid.NewGuid();
		    _LocalizedVersionGuid = Guid.NewGuid();

            //Default Language Guid should be initialised to a null Guid
		    _DefaultLanguageGuid = Null.NullGuid;
		}

		[XmlElement("portalid")]
		public int PortalID
		{
			get { return _PortalID; }
			set { _PortalID = value; }
		}
		[XmlElement("title")]
		public string ModuleTitle
		{
			get { return _ModuleTitle; }
			set { _ModuleTitle = value; }
		}
		[XmlElement("alltabs")]
		public bool AllTabs
		{
			get { return _AllTabs; }
			set { _AllTabs = value; }
		}
		[XmlElement("isdeleted")]
		public bool IsDeleted
		{
			get { return _IsDeleted; }
			set { _IsDeleted = value; }
		}
		[XmlElement("inheritviewpermissions")]
		public bool InheritViewPermissions
		{
			get { return _InheritViewPermissions; }
			set { _InheritViewPermissions = value; }
		}
		[XmlElement("header")]
		public string Header
		{
			get { return _Header; }
			set { _Header = value; }
		}
		[XmlElement("footer")]
		public string Footer
		{
			get { return _Footer; }
			set { _Footer = value; }
		}
		[XmlElement("startdate")]
		public System.DateTime StartDate
		{
			get { return _StartDate; }
			set { _StartDate = value; }
		}
		[XmlElement("enddate")]
		public System.DateTime EndDate
		{
			get { return _EndDate; }
			set { _EndDate = value; }
		}
		[XmlElement("tabmoduleid")]
		public int TabModuleID
		{
			get { return _TabModuleID; }
			set { _TabModuleID = value; }
		}
		[XmlElement("panename")]
		public string PaneName
		{
			get { return _PaneName; }
			set { _PaneName = value; }
		}
		[XmlElement("moduleorder")]
		public int ModuleOrder
		{
			get { return _ModuleOrder; }
			set { _ModuleOrder = value; }
		}
		[XmlElement("cachetime")]
		public int CacheTime
		{
			get { return _CacheTime; }
			set { _CacheTime = value; }
		}
		[XmlElement("cachemethod")]
		public string CacheMethod
		{
			get { return _CacheMethod; }
			set { _CacheMethod = value; }
		}
		[XmlElement("alignment")]
		public string Alignment
		{
			get { return _Alignment; }
			set { _Alignment = value; }
		}
		[XmlElement("color")]
		public string Color
		{
			get { return _Color; }
			set { _Color = value; }
		}
		[XmlElement("border")]
		public string Border
		{
			get { return _Border; }
			set { _Border = value; }
		}
		[XmlElement("iconfile")]
		public string IconFile
		{
			get { return _IconFile; }
			set { _IconFile = value; }
		}
		[XmlElement("visibility")]
		public VisibilityState Visibility
		{
			get { return _Visibility; }
			set { _Visibility = value; }
		}
		[XmlElement("containersrc")]
		public string ContainerSrc
		{
			get { return _ContainerSrc; }
			set { _ContainerSrc = value; }
		}
		[XmlElement("displaytitle")]
		public bool DisplayTitle
		{
			get { return _DisplayTitle; }
			set { _DisplayTitle = value; }
		}
		[XmlElement("displayprint")]
		public bool DisplayPrint
		{
			get { return _DisplayPrint; }
			set { _DisplayPrint = value; }
		}
		[XmlElement("displaysyndicate")]
		public bool DisplaySyndicate
		{
			get { return _DisplaySyndicate; }
			set { _DisplaySyndicate = value; }
		}
		[XmlElement("iswebslice")]
		public bool IsWebSlice
		{
			get { return _IsWebSlice; }
			set { _IsWebSlice = value; }
		}
		[XmlElement("webslicetitle")]
		public string WebSliceTitle
		{
			get { return _WebSliceTitle; }
			set { _WebSliceTitle = value; }
		}
		[XmlElement("websliceexpirydate")]
		public DateTime WebSliceExpiryDate
		{
			get { return _WebSliceExpiryDate; }
			set { _WebSliceExpiryDate = value; }
		}
		[XmlElement("webslicettl")]
		public int WebSliceTTL
		{
			get { return _WebSliceTTL; }
			set { _WebSliceTTL = value; }
		}
		[XmlIgnore()]
		public bool HideAdminBorder
		{
			get
			{
				object setting = TabModuleSettings["hideadminborder"];
				if (setting == null || string.IsNullOrEmpty(setting.ToString()))
				{
					return false;
				}

				bool val = false;
				bool.TryParse(setting.ToString(), out val);
				return val;
			}
		}

        [XmlElement("uniqueId")]
        public Guid UniqueId
        {
            get { return _UniqueId; }
            set { _UniqueId = value; }
        }

        [XmlElement("versionGuid")]
        public Guid VersionGuid
        {
            get { return _VersionGuid; }
            set { _VersionGuid = value; }
        }

        #region "Tab Properties"

        public TabInfo ParentTab
        {
            get
            {
                if (_ParentTab == null)
                {
                    TabController tabCtrl = new TabController();
                    if (PortalID == Null.NullInteger || string.IsNullOrEmpty(CultureCode))
                    {
                        _ParentTab = tabCtrl.GetTab(TabID, PortalID, false);
                    }
                    else
                    {
                        Locale locale = LocaleController.Instance.GetLocale(CultureCode);
                        _ParentTab = tabCtrl.GetTabByCulture(TabID, PortalID, locale);
                    }
                }
                return _ParentTab;
            }
        }

        #endregion


		[XmlIgnore()]
		public int DesktopModuleID
		{
			get { return _DesktopModuleID; }
			set { _DesktopModuleID = value; }
		}
		[XmlIgnore()]
		public DesktopModuleInfo DesktopModule
		{
			get
			{
				if (_DesktopModule == null)
				{
					if (DesktopModuleID > Null.NullInteger)
					{
						_DesktopModule = DesktopModuleController.GetDesktopModule(DesktopModuleID, PortalID);
					}
					else
					{
						_DesktopModule = new DesktopModuleInfo();
					}
				}
				return _DesktopModule;
			}
		}
		[XmlIgnore()]
		public int ModuleDefID
		{
			get { return _ModuleDefID; }
			set { _ModuleDefID = value; }
		}
		[XmlIgnore()]
		public ModuleDefinitionInfo ModuleDefinition
		{
			get
			{
				if (_ModuleDefinition == null)
				{
					if (ModuleDefID > Null.NullInteger)
					{
						_ModuleDefinition = ModuleDefinitionController.GetModuleDefinitionByID(ModuleDefID);
					}
					else
					{
						_ModuleDefinition = new ModuleDefinitionInfo();
					}
				}
				return _ModuleDefinition;
			}
		}
		[XmlIgnore()]
		public int ModuleControlId
		{
			get { return _ModuleControlId; }
			set { _ModuleControlId = value; }
		}
		public ModuleControlInfo ModuleControl
		{
			get
			{
				if (_ModuleControl == null)
				{
					if (ModuleControlId > Null.NullInteger)
					{
						_ModuleControl = ModuleControlController.GetModuleControl(ModuleControlId);
					}
					else
					{
						_ModuleControl = new ModuleControlInfo();
					}
				}
				return _ModuleControl;
			}
		}
		[XmlArray("modulepermissions"), XmlArrayItem("permission")]
		public ModulePermissionCollection ModulePermissions
		{
			get
			{
				if (_ModulePermissions == null)
				{
					_ModulePermissions = new ModulePermissionCollection(ModulePermissionController.GetModulePermissions(ModuleID, TabID));
				}
				return _ModulePermissions;
			}
			set { _ModulePermissions = value; }
		}
		[XmlIgnore()]
		public Hashtable ModuleSettings
		{
			get
			{
				if (_ModuleSettings == null)
				{
					if (ModuleID == Null.NullInteger)
					{
						_ModuleSettings = new Hashtable();
					}
					else
					{
						ModuleController oModuleCtrl = new ModuleController();
						_ModuleSettings = oModuleCtrl.GetModuleSettings(ModuleID);
						oModuleCtrl = null;
					}
				}
				return _ModuleSettings;
			}
		}
		[XmlIgnore()]
		public Hashtable TabModuleSettings
		{
			get
			{
				if (_TabModuleSettings == null)
				{
					if (TabModuleID == Null.NullInteger)
					{
						_TabModuleSettings = new Hashtable();
					}
					else
					{
						ModuleController oModuleCtrl = new ModuleController();
						_TabModuleSettings = oModuleCtrl.GetTabModuleSettings(TabModuleID);
						oModuleCtrl = null;
					}
				}
				return _TabModuleSettings;
			}
		}

        #region "Localization Properties"

        [XmlElement("cultureCode")]
        public string CultureCode
        {
            get { return _CultureCode; }
            set { _CultureCode = value; }
        }

        [XmlElement("defaultLanguageGuid")]
        public Guid DefaultLanguageGuid
        {
            get { return _DefaultLanguageGuid; }
            set { _DefaultLanguageGuid = value; }
        }

        [XmlIgnore()]
        public ModuleInfo DefaultLanguageModule
        {
            get {
		if (_DefaultLanguageModule == null && (!DefaultLanguageGuid.Equals(Null.NullGuid)) && ParentTab != null && ParentTab.DefaultLanguageTab != null && ParentTab.DefaultLanguageTab.ChildModules != null) {
			_DefaultLanguageModule = (from kvp in ParentTab.DefaultLanguageTab.ChildModules
                                                       where kvp.Value.UniqueId == DefaultLanguageGuid
                                                       select kvp.Value).SingleOrDefault();
		}
		return _DefaultLanguageModule;
	}
        }

        public bool IsDefaultLanguage
        {
            get { return (DefaultLanguageGuid == Null.NullGuid); }
        }

        public bool IsLocalized
        {
            get
            {
                bool _IsLocalized = true;
                if (DefaultLanguageModule != null)
                {
                    //Child language
                    _IsLocalized = !(ModuleID == DefaultLanguageModule.ModuleID);
                }
                return _IsLocalized;
            }
        }

        public bool IsNeutralCulture
        {
            get { return string.IsNullOrEmpty(CultureCode); }
        }

        [XmlIgnore()]
        public bool IsTranslated
        {
            get
            {
                bool _IsTranslated = true;
                if (DefaultLanguageModule != null)
                {
                    //Child language
                    _IsTranslated = (LocalizedVersionGuid == DefaultLanguageModule.LocalizedVersionGuid);
                }
                return _IsTranslated;
            }
        }

        [XmlIgnore()]
        public Dictionary<string, ModuleInfo> LocalizedModules
        {
            get
            {
                if (_LocalizedModules == null && (DefaultLanguageGuid.Equals(Null.NullGuid)) && ParentTab != null && ParentTab.LocalizedTabs != null)
                {
                    //Cycle through all localized tabs looking for this module
                    _LocalizedModules = new Dictionary<string, ModuleInfo>();
                    foreach (TabInfo t in ParentTab.LocalizedTabs.Values)
                    {
                        foreach (ModuleInfo m in t.ChildModules.Values)
                        {
                            if (m.DefaultLanguageGuid == UniqueId && !m.IsDeleted)
                            {
                                _LocalizedModules.Add(m.CultureCode, m);
                            }
                        }
                    }
                }
                return _LocalizedModules;
            }
        }

        [XmlElement("localizedVersionGuid")]
        public Guid LocalizedVersionGuid
        {
            get { return _LocalizedVersionGuid; }
            set { _LocalizedVersionGuid = value; }
        }

        #endregion


		[XmlIgnore()]
		public string ContainerPath
		{
			get { return _ContainerPath; }
			set { _ContainerPath = value; }
		}
		[XmlIgnore()]
		public int PaneModuleIndex
		{
			get { return _PaneModuleIndex; }
			set { _PaneModuleIndex = value; }
		}
		[XmlIgnore()]
		public int PaneModuleCount
		{
			get { return _PaneModuleCount; }
			set { _PaneModuleCount = value; }
		}
		[XmlIgnore()]
		public bool IsDefaultModule
		{
			get { return _IsDefaultModule; }
			set { _IsDefaultModule = value; }
		}
		[XmlIgnore()]
		public bool AllModules
		{
			get { return _AllModules; }
			set { _AllModules = value; }
		}
		public ModuleInfo Clone()
		{
			ModuleInfo objModuleInfo = new ModuleInfo();
			objModuleInfo.PortalID = this.PortalID;
			objModuleInfo.TabID = this.TabID;
			objModuleInfo.TabModuleID = this.TabModuleID;
			objModuleInfo.ModuleID = this.ModuleID;
			objModuleInfo.ModuleOrder = this.ModuleOrder;
			objModuleInfo.PaneName = this.PaneName;
			objModuleInfo.ModuleTitle = this.ModuleTitle;
			objModuleInfo.CacheTime = this.CacheTime;
			objModuleInfo.CacheMethod = this.CacheMethod;
			objModuleInfo.Alignment = this.Alignment;
			objModuleInfo.Color = this.Color;
			objModuleInfo.Border = this.Border;
			objModuleInfo.IconFile = this.IconFile;
			objModuleInfo.AllTabs = this.AllTabs;
			objModuleInfo.Visibility = this.Visibility;
			objModuleInfo.IsDeleted = this.IsDeleted;
			objModuleInfo.Header = this.Header;
			objModuleInfo.Footer = this.Footer;
			objModuleInfo.StartDate = this.StartDate;
			objModuleInfo.EndDate = this.EndDate;
			objModuleInfo.ContainerSrc = this.ContainerSrc;
			objModuleInfo.DisplayTitle = this.DisplayTitle;
			objModuleInfo.DisplayPrint = this.DisplayPrint;
			objModuleInfo.DisplaySyndicate = this.DisplaySyndicate;
			objModuleInfo.IsWebSlice = this.IsWebSlice;
			objModuleInfo.WebSliceTitle = this.WebSliceTitle;
			objModuleInfo.WebSliceExpiryDate = this.WebSliceExpiryDate;
			objModuleInfo.WebSliceTTL = this.WebSliceTTL;
			objModuleInfo.InheritViewPermissions = this.InheritViewPermissions;
			objModuleInfo.DesktopModuleID = this.DesktopModuleID;
			objModuleInfo.ModuleDefID = this.ModuleDefID;
			objModuleInfo.ModuleControlId = this.ModuleControlId;
			objModuleInfo.ContainerPath = this.ContainerPath;
			objModuleInfo.PaneModuleIndex = this.PaneModuleIndex;
			objModuleInfo.PaneModuleCount = this.PaneModuleCount;
			objModuleInfo.IsDefaultModule = this.IsDefaultModule;
			objModuleInfo.AllModules = this.AllModules;

            objModuleInfo.ContentItemId = this.ContentItemId;

            //localized properties
		    objModuleInfo.UniqueId = Guid.NewGuid();
		    objModuleInfo.VersionGuid = Guid.NewGuid();
		    objModuleInfo.DefaultLanguageGuid = DefaultLanguageGuid;
		    objModuleInfo.LocalizedVersionGuid = LocalizedVersionGuid;
		    objModuleInfo.CultureCode = CultureCode;

			return objModuleInfo;
		}
		public string GetEffectiveCacheMethod()
		{
			string effectiveCacheMethod;
			if (!string.IsNullOrEmpty(_CacheMethod))
			{
				effectiveCacheMethod = _CacheMethod;
			}
			else if (!string.IsNullOrEmpty(Entities.Host.Host.ModuleCachingMethod))
			{
				effectiveCacheMethod = Entities.Host.Host.ModuleCachingMethod;
			}
			else
			{
				DotNetNuke.Services.ModuleCache.ModuleCachingProvider defaultModuleCache = DotNetNuke.ComponentModel.ComponentFactory.GetComponent<DotNetNuke.Services.ModuleCache.ModuleCachingProvider>();
				effectiveCacheMethod = (from provider in DotNetNuke.Services.ModuleCache.ModuleCachingProvider.GetProviderList()
										where provider.Value.Equals(defaultModuleCache)
										select provider.Key).SingleOrDefault();
			}
			if (string.IsNullOrEmpty(effectiveCacheMethod))
			{
				throw new InvalidOperationException(DotNetNuke.Services.Localization.Localization.GetString("EXCEPTION_ModuleCacheMissing"));
			}
			return effectiveCacheMethod;
		}
		public void Initialize(int PortalId)
		{
			_PortalID = PortalId;
			//_TabID = -1;
			//_ModuleID = -1;
            _ModuleDefID = Null.NullInteger;
            _ModuleOrder = Null.NullInteger;
            _PaneName = Null.NullString;
            _ModuleTitle = Null.NullString;
			_CacheTime = 0;
            _CacheMethod = Null.NullString;
            _Alignment = Null.NullString;
            _Color = Null.NullString;
            _Border = Null.NullString;
            _IconFile = Null.NullString;
            _AllTabs = Null.NullBoolean;
			_Visibility = VisibilityState.Maximized;
            _IsDeleted = Null.NullBoolean;
            _Header = Null.NullString;
            _Footer = Null.NullString;
			_StartDate = Null.NullDate;
			_EndDate = Null.NullDate;
			_DisplayTitle = true;
			_DisplayPrint = false;
            _DisplaySyndicate = Null.NullBoolean;
            _IsWebSlice = Null.NullBoolean;
			_WebSliceTitle = "";
			_WebSliceExpiryDate = Null.NullDate;
			_WebSliceTTL = 0;
            _InheritViewPermissions = Null.NullBoolean;
            _ContainerSrc = Null.NullString;
            _DesktopModuleID = Null.NullInteger;
            _ModuleControlId = Null.NullInteger;
            _ContainerPath = Null.NullString;
			_PaneModuleIndex = 0;
			_PaneModuleCount = 0;
            _IsDefaultModule = Null.NullBoolean;
            _AllModules = Null.NullBoolean;
			if (PortalSettings.Current.DefaultModuleId > Null.NullInteger && PortalSettings.Current.DefaultTabId > Null.NullInteger)
			{
				ModuleController objModules = new ModuleController();
				ModuleInfo objModule = objModules.GetModule(PortalSettings.Current.DefaultModuleId, PortalSettings.Current.DefaultTabId, true);
				if (objModule != null)
				{
					_Alignment = objModule.Alignment;
					_Color = objModule.Color;
					_Border = objModule.Border;
					_IconFile = objModule.IconFile;
					_Visibility = objModule.Visibility;
					_ContainerSrc = objModule.ContainerSrc;
					_DisplayTitle = objModule.DisplayTitle;
					_DisplayPrint = objModule.DisplayPrint;
					_DisplaySyndicate = objModule.DisplaySyndicate;
				}
			}
		}
		public override void Fill(System.Data.IDataReader dr)
		{
            //Call the base classes fill method to populate base class properties
            base.FillInternal(dr);

		    UniqueId = Null.SetNullGuid(dr["UniqueId"]);
		    VersionGuid = Null.SetNullGuid(dr["VersionGuid"]);
		    DefaultLanguageGuid = Null.SetNullGuid(dr["DefaultLanguageGuid"]);
		    LocalizedVersionGuid = Null.SetNullGuid(dr["LocalizedVersionGuid"]);
		    CultureCode = Null.SetNullString(dr["CultureCode"]);

			PortalID = Null.SetNullInteger(dr["PortalID"]);
			ModuleDefID = Null.SetNullInteger(dr["ModuleDefID"]);
			ModuleTitle = Null.SetNullString(dr["ModuleTitle"]);
			AllTabs = Null.SetNullBoolean(dr["AllTabs"]);
			IsDeleted = Null.SetNullBoolean(dr["IsDeleted"]);
			InheritViewPermissions = Null.SetNullBoolean(dr["InheritViewPermissions"]);
			Header = Null.SetNullString(dr["Header"]);
			Footer = Null.SetNullString(dr["Footer"]);
			StartDate = Null.SetNullDateTime(dr["StartDate"]);
			EndDate = Null.SetNullDateTime(dr["EndDate"]);
			try
			{
				TabModuleID = Null.SetNullInteger(dr["TabModuleID"]);
				ModuleOrder = Null.SetNullInteger(dr["ModuleOrder"]);
				PaneName = Null.SetNullString(dr["PaneName"]);
				CacheTime = Null.SetNullInteger(dr["CacheTime"]);
				CacheMethod = Null.SetNullString(dr["CacheMethod"]);
				Alignment = Null.SetNullString(dr["Alignment"]);
				Color = Null.SetNullString(dr["Color"]);
				Border = Null.SetNullString(dr["Border"]);
				IconFile = Null.SetNullString(dr["IconFile"]);
				int visible = Null.SetNullInteger(dr["Visibility"]);
				if (visible == Null.NullInteger)
				{
					Visibility = VisibilityState.Maximized;
				}
				else
				{
					switch (visible)
					{
						case 0:
							Visibility = VisibilityState.Maximized;
							break;
						case 1:
							Visibility = VisibilityState.Minimized;
							break;
						case 2:
							Visibility = VisibilityState.None;
							break;

					}
				}
				ContainerSrc = Null.SetNullString(dr["ContainerSrc"]);
				DisplayTitle = Null.SetNullBoolean(dr["DisplayTitle"]);
				DisplayPrint = Null.SetNullBoolean(dr["DisplayPrint"]);
				DisplaySyndicate = Null.SetNullBoolean(dr["DisplaySyndicate"]);
				IsWebSlice = Null.SetNullBoolean(dr["IsWebSlice"]);
				if (IsWebSlice)
				{
					WebSliceTitle = Null.SetNullString(dr["WebSliceTitle"]);
					WebSliceExpiryDate = Null.SetNullDateTime(dr["WebSliceExpiryDate"]);
					WebSliceTTL = Null.SetNullInteger(dr["WebSliceTTL"]);
				}
				DesktopModuleID = Null.SetNullInteger(dr["DesktopModuleID"]);
				ModuleControlId = Null.SetNullInteger(dr["ModuleControlID"]);
			}
			catch (Exception ex)
			{
			}
		}
		[XmlIgnore()]
		public override int KeyID
		{
			get { return ModuleID; }
			set { ModuleID = value; }
		}
		public string GetProperty(string strPropertyName, string strFormat, System.Globalization.CultureInfo formatProvider, Users.UserInfo AccessingUser, Scope CurrentScope, ref bool PropertyNotFound)
		{
			string OutputFormat = string.Empty;
			if (strFormat == string.Empty)
				OutputFormat = "g";
			if (CurrentScope == Scope.NoSettings) { PropertyNotFound = true; return PropertyAccess.ContentLocked; }
			PropertyNotFound = true;
			string result = string.Empty;
			bool PublicProperty = true;
			switch (strPropertyName.ToLower())
			{
				case "portalid":
					PublicProperty = true;
					PropertyNotFound = false;
					result = (this.PortalID.ToString(OutputFormat, formatProvider));
					break;
				case "tabid":
					PublicProperty = true;
					PropertyNotFound = false;
					result = (this.TabID.ToString(OutputFormat, formatProvider));
					break;
				case "tabmoduleid":
					PublicProperty = true;
					PropertyNotFound = false;
					result = (this.TabModuleID.ToString(OutputFormat, formatProvider));
					break;
				case "moduleid":
					PublicProperty = true;
					PropertyNotFound = false;
					result = (this.ModuleID.ToString(OutputFormat, formatProvider));
					break;
				case "moduledefid":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (this.ModuleDefID.ToString(OutputFormat, formatProvider));
					break;
				case "moduleorder":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (this.ModuleOrder.ToString(OutputFormat, formatProvider));
					break;
				case "panename":
					PublicProperty = true;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.PaneName, strFormat);
					break;
				case "moduletitle":
					PublicProperty = true;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.ModuleTitle, strFormat);
					break;
				case "cachetime":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (this.CacheTime.ToString(OutputFormat, formatProvider));
					break;
				case "cachemethod":
					PublicProperty = false;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.CacheMethod, strFormat);
					break;
				case "alignment":
					PublicProperty = true;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.Alignment, strFormat);
					break;
				case "color":
					PublicProperty = true;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.Color, strFormat);
					break;
				case "border":
					PublicProperty = true;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.Border, strFormat);
					break;
				case "iconfile":
					PublicProperty = true;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.IconFile, strFormat);
					break;
				case "alltabs":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (PropertyAccess.Boolean2LocalizedYesNo(this.AllTabs, formatProvider));
					break;
				case "isdeleted":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (PropertyAccess.Boolean2LocalizedYesNo(this.IsDeleted, formatProvider));
					break;
				case "header":
					PublicProperty = true;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.Header, strFormat);
					break;
				case "footer":
					PublicProperty = true;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.Footer, strFormat);
					break;
				case "startdate":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (this.StartDate.ToString(OutputFormat, formatProvider));
					break;
				case "enddate":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (this.EndDate.ToString(OutputFormat, formatProvider));
					break;
				case "containersrc":
					PublicProperty = false;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.ContainerSrc, strFormat);
					break;
				case "displaytitle":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (PropertyAccess.Boolean2LocalizedYesNo(this.DisplayTitle, formatProvider));
					break;
				case "displayprint":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (PropertyAccess.Boolean2LocalizedYesNo(this.DisplayPrint, formatProvider));
					break;
				case "displaysyndicate":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (PropertyAccess.Boolean2LocalizedYesNo(this.DisplaySyndicate, formatProvider));
					break;
				case "iswebslice":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (PropertyAccess.Boolean2LocalizedYesNo(this.IsWebSlice, formatProvider));
					break;
				case "webslicetitle":
					PublicProperty = false;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.WebSliceTitle, strFormat);
					break;
				case "websliceexpirydate":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (this.WebSliceExpiryDate.ToString(OutputFormat, formatProvider));
					break;
				case "webslicettl":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (this.WebSliceTTL.ToString(OutputFormat, formatProvider));
					break;
				case "inheritviewpermissions":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (PropertyAccess.Boolean2LocalizedYesNo(this.InheritViewPermissions, formatProvider));
					break;
				case "desktopmoduleid":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (this.DesktopModuleID.ToString(OutputFormat, formatProvider));
					break;
				case "friendlyname":
					PublicProperty = true;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.DesktopModule.FriendlyName, strFormat);
					break;
				case "foldername":
					PublicProperty = true;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.DesktopModule.FolderName, strFormat);
					break;
				case "description":
					PublicProperty = true;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.DesktopModule.Description, strFormat);
					break;
				case "version":
					PublicProperty = true;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.DesktopModule.Version, strFormat);
					break;
				case "ispremium":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (PropertyAccess.Boolean2LocalizedYesNo(this.DesktopModule.IsPremium, formatProvider));
					break;
				case "isadmin":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (PropertyAccess.Boolean2LocalizedYesNo(this.DesktopModule.IsAdmin, formatProvider));
					break;
				case "businesscontrollerclass":
					PublicProperty = true;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.DesktopModule.BusinessControllerClass, strFormat);
					break;
				case "modulename":
					PublicProperty = true;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.DesktopModule.ModuleName, strFormat);
					break;
				case "supportedfeatures":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (this.DesktopModule.SupportedFeatures.ToString(OutputFormat, formatProvider));
					break;
				case "compatibleversions":
					PublicProperty = false;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.DesktopModule.CompatibleVersions, strFormat);
					break;
				case "dependencies":
					PublicProperty = false;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.DesktopModule.Dependencies, strFormat);
					break;
				case "permissions":
					PublicProperty = false;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.DesktopModule.Permissions, strFormat);
					break;
				case "defaultcachetime":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (this.ModuleDefinition.DefaultCacheTime.ToString(OutputFormat, formatProvider));
					break;
				case "modulecontrolid":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (this.ModuleControlId.ToString(OutputFormat, formatProvider));
					break;
				case "controlsrc":
					PublicProperty = false;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.ModuleControl.ControlSrc, strFormat);
					break;
				case "controltitle":
					PublicProperty = true;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.ModuleControl.ControlTitle, strFormat);
					break;
				case "helpurl":
					PublicProperty = true;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.ModuleControl.HelpURL, strFormat);
					break;
				case "supportspartialrendering":
					PublicProperty = true;
					PropertyNotFound = false;
					result = (PropertyAccess.Boolean2LocalizedYesNo(this.ModuleControl.SupportsPartialRendering, formatProvider));
					break;
				case "containerpath":
					PublicProperty = false;
					PropertyNotFound = false;
					result = PropertyAccess.FormatString(this.ContainerPath, strFormat);
					break;
				case "panemoduleindex":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (this.PaneModuleIndex.ToString(OutputFormat, formatProvider));
					break;
				case "panemodulecount":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (this.PaneModuleCount.ToString(OutputFormat, formatProvider));
					break;
				case "isdefaultmodule":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (PropertyAccess.Boolean2LocalizedYesNo(this.IsDefaultModule, formatProvider));
					break;
				case "allmodules":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (PropertyAccess.Boolean2LocalizedYesNo(this.AllModules, formatProvider));
					break;
				case "isportable":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (PropertyAccess.Boolean2LocalizedYesNo(this.DesktopModule.IsPortable, formatProvider));
					break;
				case "issearchable":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (PropertyAccess.Boolean2LocalizedYesNo(this.DesktopModule.IsSearchable, formatProvider));
					break;
				case "isupgradeable":
					PublicProperty = false;
					PropertyNotFound = false;
					result = (PropertyAccess.Boolean2LocalizedYesNo(this.DesktopModule.IsUpgradeable, formatProvider));
					break;
			}
			if (!PublicProperty && CurrentScope != Scope.Debug)
			{
				PropertyNotFound = true;
				result = PropertyAccess.ContentLocked;
			}
			return result;
		}
		public CacheLevel Cacheability
		{
			get { return CacheLevel.fullyCacheable; }
		}
		[Obsolete("Deprecated in DNN 5.1. All permission checks are done through Permission Collections")]
		[XmlIgnore()]
		public string AuthorizedEditRoles
		{
			get
			{
				if (string.IsNullOrEmpty(_AuthorizedEditRoles))
				{
					_AuthorizedEditRoles = ModulePermissions.ToString("EDIT");
				}
				return _AuthorizedEditRoles;
			}
		}
		[Obsolete("Deprecated in DNN 5.1. All permission checks are done through Permission Collections")]
		[XmlIgnore()]
		public string AuthorizedViewRoles
		{
			get
			{
				if (string.IsNullOrEmpty(_AuthorizedViewRoles))
				{
					if (InheritViewPermissions)
					{
						_AuthorizedViewRoles = TabPermissionController.GetTabPermissions(TabID, PortalID).ToString("VIEW");
					}
					else
					{
						_AuthorizedViewRoles = ModulePermissions.ToString("VIEW");
					}
				}
				return _AuthorizedViewRoles;
			}
		}
		[Obsolete("Deprecated in DNN 5.0. Replaced by DesktopModule.ModuleName")]
		[XmlIgnore()]
		public string ModuleName
		{
			get { return DesktopModule.ModuleName; }
			set { DesktopModule.ModuleName = value; }
		}
		[Obsolete("Deprecated in DNN 5.0. Replaced by DesktopModule.FriendlyName")]
		[XmlIgnore()]
		public string FriendlyName
		{
			get { return DesktopModule.FriendlyName; }
			set { DesktopModule.FriendlyName = value; }
		}
		[Obsolete("Deprecated in DNN 5.0. Replaced by DesktopModule.FolderName")]
		[XmlIgnore()]
		public string FolderName
		{
			get { return DesktopModule.FolderName; }
			set { DesktopModule.FolderName = value; }
		}
		[Obsolete("Deprecated in DNN 5.0. Replaced by DesktopModule.Description")]
		[XmlIgnore()]
		public string Description
		{
			get { return DesktopModule.Description; }
			set { DesktopModule.Description = value; }
		}
		[Obsolete("Deprecated in DNN 5.0. Replaced by by DesktopModule.Version")]
		[XmlIgnore()]
		public string Version
		{
			get { return DesktopModule.Version; }
			set { DesktopModule.Version = value; }
		}
		[Obsolete("Deprecated in DNN 5.0. Replaced by DesktopModule.IsPremium")]
		[XmlIgnore()]
		public bool IsPremium
		{
			get { return DesktopModule.IsPremium; }
			set { DesktopModule.IsPremium = value; }
		}
		[Obsolete("Deprecated in DNN 5.0. Replaced by DesktopModule.IsAdmin")]
		[XmlIgnore()]
		public bool IsAdmin
		{
			get { return DesktopModule.IsAdmin; }
			set { DesktopModule.IsAdmin = value; }
		}
		[Obsolete("Deprecated in DNN 5.0. Replaced by DesktopModule.BusinessControllerClass")]
		[XmlIgnore()]
		public string BusinessControllerClass
		{
			get { return DesktopModule.BusinessControllerClass; }
			set { DesktopModule.BusinessControllerClass = value; }
		}
		[Obsolete("Deprecated in DNN 5.0. Replaced by DesktopModule.SupportedFeatures")]
		[XmlIgnore()]
		public int SupportedFeatures
		{
			get { return DesktopModule.SupportedFeatures; }
			set { DesktopModule.SupportedFeatures = value; }
		}
		[Obsolete("Deprecated in DNN 5.0. Replaced by DesktopModule.IsPortable")]
		[XmlIgnore()]
		public bool IsPortable
		{
			get { return DesktopModule.IsPortable; }
		}
		[Obsolete("Deprecated in DNN 5.0. Replaced by DesktopModule.IsSearchable")]
		[XmlIgnore()]
		public bool IsSearchable
		{
			get { return DesktopModule.IsSearchable; }
		}
		[Obsolete("Deprecated in DNN 5.0. Replaced by DesktopModule.IsUpgradeable")]
		[XmlIgnore()]
		public bool IsUpgradeable
		{
			get { return DesktopModule.IsUpgradeable; }
		}
		[Obsolete("Deprecated in DNN 5.0. Replaced by DesktopModule.CompatibleVersions")]
		[XmlIgnore()]
		public string CompatibleVersions
		{
			get { return DesktopModule.CompatibleVersions; }
			set { DesktopModule.CompatibleVersions = value; }
		}
		[Obsolete("Deprecated in DNN 5.0. Replaced by DesktopModule.Dependencies")]
		[XmlIgnore()]
		public string Dependencies
		{
			get { return DesktopModule.Dependencies; }
			set { DesktopModule.Dependencies = value; }
		}
		[Obsolete("Deprecated in DNN 5.0. Replaced by DesktopModule.Permisssions")]
		[XmlIgnore()]
		public string Permissions
		{
			get { return DesktopModule.Permissions; }
			set { DesktopModule.Permissions = value; }
		}
		[Obsolete("Deprecated in DNN 5.0. Replaced by ModuleDefinition.DefaultCacheTime")]
		[XmlIgnore()]
		public int DefaultCacheTime
		{
			get { return ModuleDefinition.DefaultCacheTime; }
			set { ModuleDefinition.DefaultCacheTime = value; }
		}
		[Obsolete("Deprecated in DNN 5.0. Replaced by ModuleControl.ControlSrc")]
		[XmlIgnore()]
		public string ControlSrc
		{
			get { return ModuleControl.ControlSrc; }
			set { ModuleControl.ControlSrc = value; }
		}
		[Obsolete("Deprecated in DNN 5.0. Replaced by ModuleControl.ControlType")]
		[XmlIgnore()]
		public SecurityAccessLevel ControlType
		{
			get { return ModuleControl.ControlType; }
			set { ModuleControl.ControlType = value; }
		}
		[Obsolete("Deprecated in DNN 5.0. Replaced by ModuleControl.ControlTitle")]
		[XmlIgnore()]
		public string ControlTitle
		{
			get { return ModuleControl.ControlTitle; }
			set { ModuleControl.ControlTitle = value; }
		}
		[Obsolete("Deprecated in DNN 5.0. Replaced by ModuleControl.HelpUrl")]
		[XmlIgnore()]
		public string HelpUrl
		{
			get { return ModuleControl.HelpURL; }
			set { ModuleControl.HelpURL = value; }
		}
		[Obsolete("Deprecated in DNN 5.0. Replaced by ModuleControl.SupportsPartialRendering")]
		[XmlIgnore()]
		public bool SupportsPartialRendering
		{
			get { return ModuleControl.SupportsPartialRendering; }
			set { ModuleControl.SupportsPartialRendering = value; }
		}
		[Obsolete("Deprecated in DNN 5.1.")]
		[XmlIgnore()]
		protected TabPermissionCollection TabPermissions
		{
			get
			{
				if (_TabPermissions == null)
				{
					_TabPermissions = TabPermissionController.GetTabPermissions(TabID, PortalID);
				}
				return _TabPermissions;
			}
		}
	}
}
